package com.stone.design.patterns.observer.simple;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * @author Stone
 * @version V1.0
 * @description:
 * @date 5/8/18 20:58
 * @email redsstone@163.com
 */
public class WangXiaoHua implements Observer{


    private String title;
    private String content;
    List<Observable> mObservables = new ArrayList<>();

    public WangXiaoHua(Observable observable) {
        mObservables.add(observable);
        observable.addObserver(this);
    }

    public void subscribe(Observable observable) {
        mObservables.add(observable);
        observable.addObserver(this);
    }

    public void deleteObserver(Observable observable) {

         observable.deleteObserver(this);

    }


    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof SportNews) {
            SportNews sportNews = (SportNews) o;
            title = sportNews.getTitle();
            content = sportNews.getContent();
        }

        if (o instanceof FunNews) {
            FunNews funNews = (FunNews) o;
            title = funNews.getTitle();
            content = funNews.getContent();
        }


        display();
    }

    public void display() {
        System.out.println("Current News:" +  title + ", content:"
                + content);
    }
}
